package registration;

import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.validator.*;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.util.*;
import OnlineExam.db.*;

/**
 *  Handles the User Confirmation Form.
 */


public class ConfirmRegistrationForm extends ActionForm
{

	private String emailId;
	public String getEmailId ()
	{
		return this.emailId;
	}

	public void setEmailId (String emailId)
	{
		this.emailId = emailId;
	}

	private String password;
	public String getPassword()
	{
		return this.password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}


	/** 
	 * Input validation.
	 */
	
		
	public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) 
	{

    	servlet.log("EmailId : " + emailId);      
    	servlet.log("Password:" + password);      

		ActionErrors errors = new ActionErrors();
    
  		if (emailId == null || emailId.equals("")) 
		{
      		errors.add("Email Id", new ActionMessage("error.emailId"));
    	}
		
		else if ((emailId.indexOf ("@")) == -1)
		{
			errors.add ("Not Valid Id", new ActionMessage ("error.validemailId"));
		}

		else if (password == null || password.equals("")) 
		{
      		errors.add("Password", new ActionMessage("error.password"));
    	}

		else if (password.length () < 8)
        {
            errors.add ("Too less characters ", new ActionMessage ("error.toosmall"));
        }

		else if (!isEmailIdRegistered())
		{
			errors.add ("Not Registered", new ActionMessage ("error.notregistered"));	
		}
		
		System.out.println ("error set:" + errors);
    	return errors;	
  	}
	
	
	
	/**
	 *  Checks if the username is present in the database.
	 */


	private boolean isEmailIdRegistered ()
    {
		try
		{
			List list = DBUtil.select ("User", "registration.User", "EmailId", emailId, "Password", password);
			
			return (list.size() > 0);
		}
		catch (DBException e)
		{
			System.out.println("Exception.." + e);
			return false;
		}
	}
}
